<?php

namespace App\Modules\Apicrzs\Http\Controllers\Public;

use App\Library\Junda\src\JundaLogin;
use App\Modules\Admin\Services\Order\WaterOrderRefundServices;
use App\Modules\Admin\Services\Statistic\StatisticWaterDeviceService;
use App\Modules\Apicrzs\Http\Controllers\BaseApicrzsController;
use App\Modules\Apicrzs\Services\Notify\JunDaNotifyServices;
use App\Modules\Apicrzs\Services\Notify\WxNotifyServices;
use App\Modules\Apicrzs\Services\Pay\PayNotifyServices;
use App\Modules\Apicrzs\Services\Water\WaterOrderService;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use App\Modules\Apicrzs\Services\Pay\PayWaterOrderServices;
use App\Modules\Apicrzs\Services\Public\TestServices;
use App\Modules\Admin\Services\Statistic\WaterOrderService as AWaterOrderService;

use App\Modules\Apicrzs\Services\Water\WaterDeviceService;
use Illuminate\Http\Request;

class TestControllers extends BaseApicrzsController
{

    public function test_yvan(Request $request)
    {
        $params = $request->all();
        if (!isset($params['type'])){
            return ['code' => 'error', 'message' => '失败'];
        }


        $type = $params['type'];
        switch ($type){
            case 'jd_login':

                $access_token = JundaLogin::login();
                return ['code' => 'error', 'data'=>$access_token, 'message' => '骏达登录'];
                break;
            case 'wx':
                //微信支付 回调
                $message = json_decode(file_get_contents("php://input"),true);
                $attach = json_decode($message['attach'],true);
                $out_trade_no = $message['out_trade_no'];
                $transaction_id = $message['transaction_id'];
                $type_attach= $attach['type'];
                $data = [
                    'attach' => json_decode($message['attach']),
                    'out_trade_no' => $out_trade_no,
                    'transaction_id' => $transaction_id
                ];
                $payNotifyServices= new PayNotifyServices($type_attach);
                $res =  $payNotifyServices->handleNotify($data,'weixin');


//                $WxNotify = new WxNotifyServices();
//              $res =   $WxNotify->callbackNotify(json_decode($message,true));
                return ['code' => 'error', 'data'=>$res, 'message' => '微信回调成功'];
                break;
            case 'wx_refund':
                //  api  微信退款
                $message = file_get_contents("php://input");
                $jundaService = new JunDaNotifyServices();
                $res = $jundaService->testWxRefund($message);

                return ['code' => 'error', 'data'=>$res, 'message' => '微信订单查询'];
                break;
            case 'wx_info':
                $message = file_get_contents("php://input");
                $WxNotify = new WxNotifyServices();
                $res = $WxNotify->getOrderInfo($params);
                return ['code' => 'error', 'data'=>$res, 'message' => '微信订单查询'];
                break;
            case 'wx_order':
                $waterOrder= new PayWaterOrderServices();
                $res = $waterOrder->getOrderInfo();
                return ['code' => 'error', 'data'=>$res, 'message' => '微信订单查询'];
                break;
            case 'junda':
                $message = file_get_contents("php://input");
                $jundaService = new JunDaNotifyServices();
                $res = $jundaService->callback($message);
                $message = file_get_contents("php://input");
                return ['code' => 'error', 'data'=>$res, 'message' => '骏达系统'];
                break;
            case 'junda_order':
                $order_sn = $params['order_sn'];

                WaterOrderRefundServices::instance()->getWaterOrderList();

                $info = TestServices::instance()->WeiXinByJundaByOrderSn($order_sn);
                return ['code' => 'error', 'data'=>$info, 'message' => '订单查询[微信、骏达打水状态]'];
                break;
            case 'tongji':
                $jundaService = new StatisticWaterDeviceService();
                $res = $jundaService->getWaterDeviceList($params);
                return ['code' => 'error', 'data'=>$res, 'message' => '骏达系统'];
                break;
            case 'o':
                $jundaService = new AWaterOrderService(11);
                $res = $jundaService->lists($params);
                return ['code' => 'error', 'data'=>$res, 'message' => '骏达系统'];
                break;
            case 'cache_d':
                $jundaService = new WaterDeviceModel();
                $res = $jundaService->getCache();
                return ['code' => 'error', 'data'=>$res, 'message' => '骏达系统'];
                break;
            case 'WaterDevice':
                $jundaService = new WaterDeviceService(new WaterDeviceModel());
                $res = $jundaService->getCacheEquipmentCheckPay([]);
                return ['code' => 'error', 'data'=>$res, 'message' => '骏达系统'];
                break;
        }
    }
}
